package com.hitqz.robot.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.JobCachesEntity;
import com.hitqz.robot.api.business.vo.JobCachesVo;
import com.hitqz.robot.biz.mapper.JobCachesMapper;
import com.hitqz.robot.biz.service.JobCachesService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@RequiredArgsConstructor
public class JobCachesServiceImpl extends ServiceImpl<JobCachesMapper, JobCachesEntity> implements JobCachesService {


    @Override
    public Long getMaxSn(Integer groupId) {
        return baseMapper.getMaxSn(groupId);
    }

    @Override
    public List<JobCachesVo> sortType() {
        List<JobCachesVo> jobCachesVos = Lists.newArrayList();
        List<JobCachesEntity> list = this.list(Wrappers.<JobCachesEntity>lambdaQuery()
                .orderByAsc(JobCachesEntity::getSn));
        String lastName = "";
        Integer lastIndex = -1;
        for (JobCachesEntity jobCachesEntity: list) {
            if (!lastName.equals(jobCachesEntity.getTaskName())) {
                // 新记录
                lastIndex ++;
                JobCachesVo vo = new JobCachesVo();
                vo.setTaskName(jobCachesEntity.getTaskName());
                vo.setNumber(1);
                vo.setId(lastIndex +1);
                jobCachesVos.add(lastIndex, vo);
                lastName = vo.getTaskName();
            } else {
                // 旧记录
                JobCachesVo vo = jobCachesVos.get(lastIndex);
                vo.setNumber(vo.getNumber() + 1);
                lastName = vo.getTaskName();
            }
        }
        return jobCachesVos;

    }
}
